package com.cens.mall.order.listener;

import com.cens.mall.order.entity.OrderEntity;
import com.cens.mall.order.service.OrderService;
import com.cens.mall.order.to.OrderCreateTo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order-release-queue")
public class OrderCloseListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listene(OrderCreateTo order, Channel cHannel, Message message) throws IOException {
        System.out.println("order release");
        System.out.println(order);
        try {
            orderService.closeOrder(order);
            cHannel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            cHannel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
